home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2000 #5 / Amiga Plus CD - 2000 - No. 5.iso / Tools / Musik / Misc / Amster / Source / url.c < prev   
Encoding:
C/C++ Source or Header  |  2000-01-01  |  3.7 KB  |  148 lines

  1. /*
  2. ** Clickable URL Class
  3. */
  4.  
  5. #include "include/config.h"
  6.  
  7. #include <string.h>
  8.  
  9. #include "include/mui.h"
  10. #include <proto/graphics.h>
  11. #include <proto/openurl.h>
  12.  
  13. #include "include/url.h"
  14.  
  15. struct Library *OpenURLBase;
  16.  
  17. ULONG url_new(struct IClass *cl, Object *obj, struct opSet *msg);
  18. ULONG url_setup(struct IClass *cl, Object *obj, Msg msg);
  19. ULONG url_minmax(struct IClass *cl, Object *obj, struct MUIP_AskMinMax *msg);
  20. ULONG url_draw(struct IClass *cl, Object *obj, struct MUIP_Draw *msg);
  21. ULONG url_cleanup(struct IClass *cl, Object *obj, Msg msg);
  22. ULONG url_clicked(struct IClass *cl, Object *obj, Msg msg);
  23.  
  24.  
  25. MUIF url_dispatch(REG(a0) struct IClass *cl,REG(a2) Object *obj,REG(a1) Msg msg)
  26. {
  27.     switch(msg->MethodID) {
  28.         case OM_NEW:         return(url_new(cl,obj,(APTR)msg));
  29.         case MUIM_Setup:     return(url_setup(cl,obj,(APTR)msg));
  30.         case MUIM_AskMinMax: return(url_minmax(cl,obj,(APTR)msg));
  31.         case MUIM_Draw:      return(url_draw(cl,obj,(APTR)msg));
  32.         case MUIM_Cleanup:   return(url_cleanup(cl,obj,(APTR)msg));
  33.         case URL_CLICKED:    return(url_clicked(cl,obj,(APTR)msg));
  34.     }
  35.     return(DoSuperMethodA(cl,obj,msg));
  36. }
  37.  
  38.  
  39. ULONG url_new(struct IClass *cl, Object *obj, struct opSet *msg)
  40. {
  41.     struct urldata *data;
  42.     char *name, *href;
  43.  
  44.     name = (char *)GetTagData(URL_NAME,0,msg->ops_AttrList);
  45.     href = (char *)GetTagData(URL_HREF,0,msg->ops_AttrList);
  46.  
  47.     obj = (Object *)DoSuperNew(cl,obj,
  48.         MUIA_Font, MUIV_Font_Button,
  49.         MUIA_ShowSelState, FALSE,
  50.         MUIA_InputMode, MUIV_InputMode_RelVerify,
  51.         /* MUIA_ShortHelp, help, */
  52.         TAG_MORE, msg->ops_AttrList);
  53.  
  54.     if(!obj) return(NULL);
  55.  
  56.     data = INST_DATA(cl,obj);
  57.     data->name = name;
  58.     data->href = href;
  59.     data->len = strlen(name);
  60.     data->color = -1;
  61.  
  62.     DoMethod(obj,MUIM_Notify,MUIA_Pressed,FALSE,obj,1,URL_CLICKED);
  63.     return((ULONG)obj);
  64. }
  65.  
  66.  
  67. ULONG url_setup(struct IClass *cl, Object *obj, Msg msg)
  68. {
  69.     struct urldata *data = INST_DATA(cl,obj);
  70.  
  71.     if (!DoSuperMethodA(cl,obj,msg))
  72.         return(FALSE);
  73.  
  74.     data->color = ObtainBestPen(_screen(obj)->ViewPort.ColorMap,0,0,0,OBP_Precision,PRECISION_GUI,TAG_DONE);
  75.  
  76.     return(TRUE);
  77. }
  78.  
  79.  
  80. ULONG url_minmax(struct IClass *cl, Object *obj, struct MUIP_AskMinMax *msg)
  81. {
  82.     struct urldata *data = INST_DATA(cl,obj);
  83.     int x,y;
  84.  
  85.     DoSuperMethodA(cl,obj,(APTR)msg);
  86.  
  87.     x = _font(obj)->tf_XSize * data->len;
  88.     y = _font(obj)->tf_YSize;
  89.  
  90.     msg->MinMaxInfo->MinWidth  += x;
  91.     msg->MinMaxInfo->DefWidth  += x + (x/10);
  92.     msg->MinMaxInfo->MaxWidth  += MUI_MAXMAX;
  93.     msg->MinMaxInfo->MinHeight += y;
  94.     msg->MinMaxInfo->DefHeight += y;
  95.     msg->MinMaxInfo->MaxHeight += y;
  96.  
  97.     return(0);
  98. }
  99.  
  100.  
  101. ULONG url_draw(struct IClass *cl, Object *obj, struct MUIP_Draw *msg)
  102. {
  103.     struct urldata *data = INST_DATA(cl,obj);
  104.     int x,y,i;
  105.  
  106.     DoSuperMethodA(cl,obj,(APTR)msg);
  107.     if(!(msg->flags & MADF_DRAWOBJECT)) return(0);
  108.  
  109.     SetFont(_rp(obj),_font(obj));
  110.  
  111.     if(!data->pixlen) data->pixlen=TextLength(_rp(obj),data->name,data->len);
  112.     x = _mleft(obj) + ( (_mwidth(obj) - data->pixlen) / 2);
  113.     y = _mtop(obj) + _font(obj)->tf_Baseline + ( (_mheight(obj) - _font(obj)->tf_YSize) / 2 );
  114.  
  115.     Move(_rp(obj),x,y);
  116.     if(data->color != -1) SetAPen(_rp(obj),data->color); else SetAPen(_rp(obj),1);
  117.     Text(_rp(obj),data->name,data->len);
  118.  
  119.     for (i=x; i<=x+data->pixlen; i+=4) {
  120.          Move(_rp(obj), i, y+3);
  121.         Draw(_rp(obj), i+1, y+3);
  122.     }
  123.  
  124.     return(0);
  125. }
  126.  
  127.  
  128. ULONG url_cleanup(struct IClass *cl, Object *obj, Msg msg)
  129. {
  130.     struct urldata *data = INST_DATA(cl,obj);
  131.  
  132.     if(data->color != -1) ReleasePen(_screen(obj)->ViewPort.ColorMap,data->color);
  133.  
  134.     return(DoSuperMethodA(cl,obj,msg));
  135. }
  136.  
  137.  
  138. ULONG url_clicked(struct IClass *cl, Object *obj, Msg msg)
  139. {
  140.     struct urldata *data = INST_DATA(cl,obj);
  141.  
  142.     OpenURLBase = OpenLibrary("openurl.library", 1);
  143.     if (!OpenURLBase) return(0);
  144.     URL_Open(data->href, TAG_DONE);
  145.     CloseLibrary(OpenURLBase);
  146.     return(0);
  147. }
  148.